﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EasyTemplates
{
    class TemplateSignature
    {
        Dictionary<string, Type> parameterTypes = new Dictionary<string, Type>();

        public TemplateSignature(Template template)
        {
            foreach (KeyValuePair<string, Parameter> parameterPairCrt in template.Parameters)
            {
                parameterTypes.Add(
                        parameterPairCrt.Key,
                        parameterPairCrt.Value.Type
                );
            }
        }

        public bool Matches(TemplateSignature newSignature)
        {
            var newParameterTypes = newSignature.parameterTypes;

            if (newParameterTypes.Count !=
                parameterTypes.Count)
                return false;

            bool notSameParameterNames =
                parameterTypes.Keys.Any(
                    parameterNameCrt =>
                        !newParameterTypes.ContainsKey(parameterNameCrt)
                    );
            if (notSameParameterNames)
                return false;

            bool compatibleParameterTypes =
                parameterTypes.Keys.All(
                    parameterNameCrt =>
                        parameterTypes[parameterNameCrt].IsAssignableFrom(
                            newParameterTypes[parameterNameCrt]
                        )
                );
            if (!compatibleParameterTypes)
                return false;

            return true;
        }
    }
}
